package com.example.bili.view.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.BaseAdapter;

import com.example.bili.R;
import com.example.bili.adapter.PartitionLiveAdapter;
import com.example.bili.adapter.recommendedAdapter;
import com.example.bili.allbean.PartitionLiveBean;
import com.example.bili.allbean.recommendedBean;
import com.example.bili.module.OkHttp;
import com.example.bili.util.GlideImageLoader;
import com.example.bili.util.ImageLoaderConfig;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;

import static com.example.bili.internet.connectors.HOMELIVE;
import static com.example.bili.internet.connectors.RECOMMENDED;

public class LiveActivity extends AppCompatActivity implements OkHttp.DataCallBack, OnBannerListener {

    private Banner l_partition_live_banner;
    private RecyclerView l_partition_live_recyclerview;
    private List<String> liveBannerUrl;
    private List<String> liveBannerWebUrl;
    private PartitionLiveBean partitionLiveBean;
    private List<PartitionLiveBean.DataBean.EntranceIconsBean> entranceIcons;
    private List<PartitionLiveBean.DataBean.PartitionsBean> partitions;

    /**
     * 分区直播
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(getApplicationContext(), ImageLoaderConfig.getImagePipelineConfig(getApplicationContext()));
        setContentView(R.layout.activity_live);
        l_partition_live_banner = (Banner) findViewById(R.id.l_partition_live_banner);
        l_partition_live_recyclerview = (RecyclerView) findViewById(R.id.l_partition_live_recyclerview);
        liveBannerUrl = new ArrayList<>();
        liveBannerWebUrl = new ArrayList<>();
        getdata();

    }


    public void getdata() {

        OkHttp.getAsync(HOMELIVE,this);
    }

    @Override
    public void requestFailure(Request request, IOException e) {

    }

    @Override
    public void requestSuccess(String result) throws Exception {
        partitionLiveBean = new Gson().fromJson(result, PartitionLiveBean.class);
        entranceIcons = partitionLiveBean.getData().getEntranceIcons();
        partitions = partitionLiveBean.getData().getPartitions();
        getbannerurl();
    }

    @Override
    public void OnBannerClick(int position) {
        startActivity(new Intent(this, Webactivity.class).putExtra("weburl", liveBannerWebUrl.get(position)));
    }

    public void getbannerurl() {
        for (int i = 0; i < partitionLiveBean.getData().getBanner().size(); i++) {

            liveBannerUrl.add(partitionLiveBean.getData().getBanner().get(i).getImg());
            liveBannerWebUrl.add(partitionLiveBean.getData().getBanner().get(i).getLink());

        }
        initbanner();
        
        l_partition_live_recyclerview.setLayoutManager(new LinearLayoutManager(this));
        l_partition_live_recyclerview.setAdapter(new PartitionLiveAdapter(this, entranceIcons,partitions));
        
        l_partition_live_banner.setFocusable(true);
        l_partition_live_banner.setFocusableInTouchMode(true);
        l_partition_live_banner.requestFocus();
    }
    private void initbanner() {
        //图片加载器
        l_partition_live_banner.setImageLoader(new GlideImageLoader());
        //条目点击事件
        l_partition_live_banner.setOnBannerListener(this);
        //添加图片集合
        l_partition_live_banner.setImages(liveBannerUrl);
        //开始轮播
        l_partition_live_banner.start();
    }

}
